#include "ball.h"
#include <QTime>
#include "pengZhuang.h"
#include <QFont>
#include <QDebug>
#include <QMessageBox>
ball::ball(double r, double x, double y, QLabel * ql, Plank *pla)
{
    this->Bla=ql;
    this->r=r;
    this->bx=x;
    this->by=y;
    this->pla=pla;
    IsStart = false;
//  ql->setText("o");
    QPixmap px = QPixmap(":/ic/ball");
    ql->setPixmap(px);
    QFont ft;
    ft.setPointSize(17);
    ql->setFont(ft);
    ql->setGeometry(x,y,r,r);
    QTime time;
    time= QTime::currentTime();
    qsrand(time.msec()+time.second()*1000);
    int n;
    n=(-qrand()%3)+(qrand()%3);
    while(n==0)
    {
        n=(-qrand()%3+qrand()%3);
    }
    speedx = n;
    speedy=-2.01;
}
void ball::moveFollowPlank(double x)
{
    if(this->IsStart==false)
    this->Bla->setGeometry(x+93.3,by,r,r);
}
void ball::moveInBox()
{
    if(IsStart == true)
    {
        double x,y;
        x=this->Bla->geometry().x();
        y=this->Bla->geometry().y();
        if((pengZhaung::check(this))==LeftP)
            speedx = -speedx;
        if((pengZhaung::check(this))==RightP)
            speedx = -speedx;
        if((pengZhaung::check(this))==TopP)
            speedy = -speedy;
        if((pengZhaung::check(this))==PlankP)
            speedy = -speedy;
        if((pengZhaung::check(this))==FallP)
        {
            IsStart = false;
            this->speedx = 0.5*speedx;
            this->speedx = 0.5*speedy;
            QString qst1 = "游戏失败";
            QString qst2 = "小球掉落";
            QMessageBox message(QMessageBox::NoIcon, qst1,qst2);
            message.exec();
            //disconnect(tim);
        }
        y=y+speedy;
        x=x+speedx;
        this->Bla->setGeometry(x,y,r,r);
    }
    else
        this->moveFollowPlank(this->pla->getX());

}
void ball::setStyle(int n)
{
    switch(n)
    {
    case 1:
    {
        Bla->setPixmap(QPixmap("background:transparent"));
        QPixmap px = QPixmap(":/ic/ball");
        Bla->setPixmap(px);
        break;
    }
    case 2:
    {
        QPixmap px = QPixmap(":/ic/ball2");
        Bla->setPixmap(px);
        break;

    }
    /*case 3:
    {
        QPixmap px = QPixmap(":/ic/ball3");
        Bla->setPixmap(px);
        break;

    }
    case 4:
    {
        QPixmap px = QPixmap(":/ic/ball4");
        Bla->setPixmap(px);
        break;

    }
    case 5:
    {
        QPixmap px = QPixmap(":/ic/ball5");
        Bla->setPixmap(px);
        break;

    }*/
    default:
        break;
    }
}

double ball::getX(){
    return this->Bla->geometry().x()+this->getR();
}

double ball::getY(){
    return this->Bla->geometry().y()+this->getR();
}

double ball::getR(){
    return r/2;
}
void ball::debug(){
    //qDebug()<<getX();
        // qDebug()<<getY()<<getR();
}
void ball::end(){
    this->IsStart = false;
    this->speedx = 0.5*speedx;
    this->speedx = 0.5*speedy;
}

void ball::finish(){
    this->IsStart = false;
}
